package cn.yikaojiuguo.student.support.database;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.app.YiKaoApplication;
import cn.yikaojiuguo.student.bean.Category;
import cn.yikaojiuguo.student.support.utils.LogUtil;
import cn.yikaojiuguo.student.support.utils.Util;

/**
 * Created by cfan_yang on 2015/12/16.
 * 管理外部导入数据库kongfuzi.db
 */
public class MyDBManager {

    private static SQLiteDatabase mDatabase;
    private final static String DB_NAME = "kongfuzi.db";
    private final static String TABLE_NAME = "category";
    //数据库存在files目录下
    private final static String DB_PATH = YiKaoApplication.getInstance().getFilesDir().getAbsolutePath() +"/" + DB_NAME;

    public static synchronized SQLiteDatabase openDatabase() {
        if(mDatabase == null) {
            mDatabase = SQLiteDatabase.openOrCreateDatabase(DB_PATH,null);
        }
        return mDatabase;
    }

    public static void checkDatabase() {
        openDatabase();
        LogUtil.showInfoLog("database", "files目录下kongfuzi.db版本号=" + mDatabase.getVersion());
        if(mDatabase.getVersion()< Util.getVersionCode()) { //需要升级数据库
            try {
                upgradeDataBase();
                mDatabase.setVersion(Util.getVersionCode());
                LogUtil.showInfoLog("database", "升级后kongfuzi.db版本号=" + mDatabase.getVersion());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        release();
    }

    private static void upgradeDataBase() throws IOException {
        InputStream in = YiKaoApplication.getInstance().getResources().openRawResource(R.raw.kongfuzi);
        FileOutputStream fos = new FileOutputStream(DB_PATH);
        byte[] buffer = new byte[1024];
        int count = 0;
        while ((count = in.read(buffer))!=-1) {
            fos.write(buffer,0,count);
        }
        in.close();
        fos.close();
    }

    /**
     * 释放资源
     */
    public static void release() {
        if(mDatabase!=null) {
            mDatabase.close();
            mDatabase = null;
            LogUtil.showInfoLog("database","database close");
        }
    }

    /**
     * 通过evalue查询
     */
    public static List<Category> queryByEvalue(int evalue) {
        openDatabase();
        Cursor cursor = mDatabase.query(TABLE_NAME, new String[]{"id", "ename"}, "evalue=?", new
                String[]{evalue + ""}, null, null, null);
        if(cursor!=null && cursor.getCount()>0) {
            List<Category> list = new ArrayList<Category>(cursor.getCount());
            while (cursor.moveToNext()) {
                Category category = new Category();
                category.id = cursor.getInt(cursor.getColumnIndex("id"));
                category.ename = cursor.getString(cursor.getColumnIndex("ename"));
                category.evalue = evalue;
                list.add(category);
            }
            release();
            return list;
        }
        release();
        return new ArrayList<Category>(0);
    }

}
